<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/7
 * Time: 9:51
 */

namespace App\FreeBird\Repositories;

use App\Admin\Contracts\MemberInterface;
use App\FreeBird\Contracts\InfoClassInterface;
use App\FreeBird\Contracts\StoreUserInterface;
use App\FreeBird\Exceptions\FreeBirdException;
use App\FreeBird\Models\StoreUserModel;
use App\Weixin\Contracts\UserInterface;
use Illuminate\Support\Facades\Request;

class  DbStoreUserRepositories implements StoreUserInterface
{


    /**
     * 获取入驻用户模型
     * @return StoreUserModel
     */
    public function getStoreUserModel()
    {
        return app()->make(StoreUserModel::class);
    }

    /**
     * @return UserInterface
     */
    public function getUserInterface()
    {
        return app()->make(UserInterface::class);
    }

    /**
     * 获取用户接口
     * @return MemberInterface
     */
    public function getMemberInterface()
    {
        return app()->make(MemberInterface::class);
    }

    /**
     * 获取用户信息
     * @param $where
     */
    public function getList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'sort' : $request->input("sort");
        $order = empty($request->input("order")) ? 'asc' : $request->input("order");
        return $this->getStoreUserModel()->newQuery()->where($where)->orderBy($sort, $order)->paginate($limit);
    }

    /**
     * 用户添加
     * @param $data
     */
    public function setUserInfo($data)
    {
        $data['uid'] = AUID;
        $data = $this->getStoreUserModel()->filter($data);
        return $this->getStoreUserModel()->newQuery()->updateOrCreate(['uid' => $data['uid']], $data);
    }

    /**
     * 获取用户信息
     * @param $where
     */
    public function getUserInfo($where)
    {
        return $this->getStoreUserModel()->newQuery()->where($where)->first();
    }

    /**
     * 获取用户信息
     * @param $where
     */
    public function getUserInfoAndUser($where)
    {
        return $this->getStoreUserModel()->newQuery()->with("user")->where($where)->first();
    }



    public function updateUserInfo($where, $data)
    {
        return $this->getStoreUserModel()->newQuery()->where($where)->update($data);
    }

    public function updateUserInfoApply($where, $data)
    {
        if ($this->getStoreUserModel()->newQuery()->where($where)->update($data)) {
            //更新用户lid层级
            $userInfo = $this->getStoreUserModel()->newQuery()->where($where)->first();
            return $this->getUserInterface()->updateXiaoUserInfo(["uid" => $userInfo->uid], ['lid' => 2]);
        } else {
            throw  new FreeBirdException(FreeBirdException::ERROR_DATABASE);
        }
    }

    /**
     * 邦定用户手机号
     * @param $data  日期
     * @return mixed  返回
     * @throws FreeBirdException
     */
    public function bindUserMobile($data)
    {
        $mobile = $data["mobile"];
        $code = $data["code"];
        if ($this->getUserInterface()->checkMobile($mobile, $code)) {
             $this->getUserInterface()->updateXiaoUserInfo(["uid"=>AUID],["mobile" => $mobile]);
            //将手机号更新用户字段
            return $this->getMemberInterface()->updateUcenterMember(["id" => AUID], ["mobile" => $mobile]);
        } else {
            throw  new FreeBirdException(FreeBirdException::ERROR_CHECK_MOBILE_MESSAGE);
        }
    }

    /**
     * 更新用户表的用户信息
     * @param $data
     * @return mixed|void
     */
    public function updateUserAddInfo($data)
    {
        /**
         * 获取一次值
         */
        $data["class_name"] = $this->setClassAttribute($data['class']);
        return $this->getUserInterface()->updateXiaoUserInfo(["uid" => AUID],$data);
    }


    public $rarr = array();

    /**
     * 得到父级数组
     * @param int
     * @return array
     */
    public function get_parent($myid, $refer, $pidname = 'pid')
    {
        if (!isset($refer[$myid])) {
            return false;
        }
        if ($refer[$myid][$pidname] == 0) {
            return true;
        } else {
            $this->rarr[] = $refer[$refer[$myid][$pidname]];  //父类值
            $this->get_parent($refer[$myid][$pidname], $refer);
        }
    }

    /**
     * 获取行业分类
     * @return InfoClassInterface
     */
    public function getInfoClassInterface()
    {
        return app()->make(InfoClassInterface::class);
    }

    public function setClassAttribute($value)
    {
        $returnArr=explode(",",$value);
        $classNameArr=array();
        foreach ($returnArr as $k=>$v){
            $classNameArr[]=$this->getInfoClassInterface()->getInfoFieldValue(["id"=>$v],"name");
        }
        return "|" . implode("|", $classNameArr) . "|";
        //获取分类
//        $classList = $this->getInfoClassInterface()->getClassAll([]);
//        $classArr = $classList->toArray();
//
//        $newArr = array();
//        foreach ($classArr as $key => $val) {
//            $newArr[$val['id']] = $val;
//        }
//        $this->rarr[] = $newArr[$value];
//        $this->get_parent($value, $newArr);
//        if (!empty($this->rarr)) {
//            $makeArr = array();
//
//            foreach ($this->rarr as $sv) {
//                $makeArr[] = $sv['name'];
//            }
//            return "|" . implode("|", $makeArr) . "|";
//        }

    }

    /**
     * 获取用户信息
     * @param $where
     * @return mixed|void
     */
    public function wxUserInfo($where){
       return $this->getUserInterface()->getUserInfoByIdSelect(AUID,["class","class_name","max_juli","remark","up_img1","up_img2","up_img3","up_img4"]);
    }

    public function getListHasUser($where){
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'sort' : $request->input("sort");
        $order = empty($request->input("order")) ? 'asc' : $request->input("order");
        return $this->getStoreUserModel()->newQuery()->with("user")->where($where)->orderBy($sort, $order)->paginate($limit);
    }

}